package services

import (
	"context"
	"github.com/golang/protobuf/ptypes/wrappers"
	context2 "kop/framework/context"
	captainC "kop/modules/captain/client"
	questC "kop/modules/quest/client"
	ringC "kop/modules/ring/client"
	"kop/pb"
	"kop/pb/service"
)

type CaptainService struct {
	context2.Context
}

func (s *CaptainService) Piece(ctx context.Context, req *wrappers.Int32Value) (*pb.CaptainPieceResp, error) {

	return captainC.CaptainService.Piece(ctx, req)
}

func (s *CaptainService) LevelUp(ctx context.Context, req *wrappers.Int32Value) (*pb.CaptainReply, error) {

	var reply pb.CaptainReply

	var err error
	reply.Captain, err = captainC.CaptainService.LevelUp(ctx, req)
	return &reply, err
}

func (s *CaptainService) TalentLevelUp(ctx context.Context, req *pb.TalentLevelUpRequest) (*pb.TalentLevelUpResp, error) {

	return captainC.CaptainService.TalentLevelUp(ctx, req)
}

//
//// 碎片升级天赋
//func (*CaptainService) talentUpUsePiece(ctx context.Context, req *pb.TalentLevelUpRequest) (*pb.TalentLevelUpReply, error) {
//
//	var reply pb.TalentLevelUpReply
//
//	if req.CaptainID < 1 {
//		return &reply, errors.ParamError
//	}
//
//	var roleID = s.GetRoleID(ctx)
//
//	captain, err := captainC.GetCaptain(roleID, req.CaptainID)
//	if err != nil {
//		return &reply, err
//	}
//
//	var talentCopy pb.Talent
//
//	for _, talent := range captain.Talents {
//		if talent.TalentID == req.TalentID {
//
//			talentCopy = *talent
//
//			talent.Level += 1
//			break
//		}
//	}
//
//	if talentCopy.TalentID == 0 {
//		return &reply, errors.ParamError
//	}
//
//	if talentCopy.Level >= talentCopy.MaxLevel {
//		return &reply, errors.ParamError
//	}
//
//	reply.Item, err = itemC.SubItem(roleID, nil, pb.RawItem{ItemID: req.ItemID, Number: 1})
//	if err != nil {
//		return &reply, err
//	}
//
//	captainC.Save(roleID, captain)
//	reply.Captain = captain
//	reply.Success = true
//
//	if captain.BuildingID > 0 {
//		reply.Building = buildingC.ResetCoin(roleID, captain.BuildingID)
//	}
//
//	// 主线任务
//	questC.Power(roleID)
// _ = roleC.UpdatePower(roleID, captainC.SumPower(roleID))
//	return &reply, nil
//}

// 船长升级技能
// 参数 req.SkillIndex 2 - 3
// 消耗船长Exp
func (s *CaptainService) SkillLevelUp(ctx context.Context, req *pb.CaptainAPISkillLevelUpRequest) (*pb.Captain, error) {

	var roleID = s.GetRoleID(ctx)

	var captain, err = captainC.SkillLevelUp(roleID, req.CaptainID, req.SkillIndex)
	if err != nil {
		return captain, err
	}

	// 主线任务
	questC.CaptainSkillLevelUp(roleID)

	return captain, nil
}

// 船长晋升
func (s *CaptainService) ClassUp(ctx context.Context, req *wrappers.Int32Value) (*pb.CaptainClassUpReply, error) {

	var reply pb.CaptainClassUpReply

	var err error
	reply.Captain, err = captainC.CaptainService.ClassUp(ctx, req)
	return &reply, err
}

func (s *CaptainService) Equip(ctx context.Context, req *pb.CaptainAPIEquipRequest) (*pb.EquipReply, error) {

	var reply pb.EquipReply

	var roleID = s.GetRoleID(ctx)
	var err error

	var ring = &pb.Ring{}
	if req.RingUniqueID != 0 {

		if ring, err = ringC.GetRing(roleID, req.RingUniqueID); err != nil {
			return &reply, err
		}
	}

	reply.Captain, err = captainC.CaptainService.Equip(ctx, &service.CaptainEquipRequest{CaptainID: req.CaptainID, Ring: ring})

	return &reply, err
}

func (s *CaptainService) Maid(ctx context.Context, req *pb.CaptainAPIMaidRequest) (*pb.EquipReply, error) {

	var reply pb.EquipReply

	var err error
	reply.Captain, err = captainC.CaptainService.Maid(ctx, req)

	return &reply, err
}

func (s *CaptainService) Ready(ctx context.Context, req *pb.CaptainReadyRequest) (*pb.Captain, error) {

	return captainC.Ready(s.GetRoleID(ctx), req.CaptainID, req.Ready)
}
